import { defineConfig } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import { join } from "path";
import { fileURLToPath } from "url";
import ElementPlus from "unplugin-element-plus/vite";
import vuePlugin from "@vitejs/plugin-vue";

function resolve(dir: string) {
  return join(__dirname, dir);
}
const root = resolve("src");

// https://vite.dev/config/
export default defineConfig({
  root,
  resolve: {
    alias: {
      "@src": root,
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  base: "/",

  build: {
    rollupOptions: {
      output: {
        // 在这里修改静态资源路径
        chunkFileNames: "demoA/assets/js/[name]-[hash].js",
        entryFileNames: "demoA/assets/js/[name]-[hash].js",
        assetFileNames(assetInfo) {
          const imgExts = [".png", ".jpg", ".jpeg", ".ico", ".svg", "gif"];

          if (imgExts.some((ext) => assetInfo.name?.endsWith(ext))) {
            return "demoA/assets/imgs/[name]-[hash].[ext]";
          }
          return "demoA/assets/css/[name]-[hash].[ext]";
        },
      },
    },
  },

  plugins: [
    vuePlugin(),
    ElementPlus({}),
    AutoImport({
      dts: resolve(`custom-types/auto-imports.d.ts`),
      imports: ["vue"],
    }),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData:
          '@use "@/styles/mixins.scss" as *; @use "@/styles/variables.scss" as *;',
      },
    },
  },
});
